﻿using ProjScan.Modules;
using ProjScan.Tools;
using System;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace ProjScan.Submodules.Effects_Play_Check
{
    public class OverDrawUrpSupport
    {
        private MethodInfo setRendererFeatureInfo;

        private Type urpAssetSetupType;

        private MethodInfo setSrpSettingInfo;

        private MethodInfo removeOverDrawFeatureInfo;

        private Assembly urpFeatureLibAssembly;

        public object[] urpDataPath;

        public object[] urpAssetPath;

        private string urpFeatureLibFileName;

        private object urpAssetSetupInstance;

        private void LoadUrpPaths()
        {
            urpDataPath = new object[1]
            {
                AssetDatabase.GUIDToAssetPath(AssetDatabase.FindAssets("ProjScanOverDrawRendererData")[0])
            };
            urpAssetPath = new object[1]
            {
                AssetDatabase.GUIDToAssetPath(AssetDatabase.FindAssets("ProjScanOverDrawRendererAsset")[0])
            };
        }

        private static Assembly ResolveAssembly(object _param0, ResolveEventArgs _param1)
        {
            Debug.Log("Resolving...");
            return typeof(AssetDatabase).Assembly;
        }

        public void LoadUrpAssembly()
        {
            try
            {
                LoadUrpPaths();
                if (File.Exists(Directory.GetFiles(Environment.CurrentDirectory, urpFeatureLibFileName, SearchOption.AllDirectories)[0]))
                {
                    urpFeatureLibAssembly = Assembly.LoadFrom(Directory.GetFiles(Environment.CurrentDirectory, urpFeatureLibFileName, SearchOption.AllDirectories)[0]);
                    AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
                }

                urpAssetSetupType = urpFeatureLibAssembly.GetType("UrpFeatureLib.UrpAssetSetUp");
                setRendererFeatureInfo = urpAssetSetupType.GetMethod("SetRendererFeature");
                setSrpSettingInfo = urpAssetSetupType.GetMethod("SetSrpSetting");
                removeOverDrawFeatureInfo = urpAssetSetupType.GetMethod("RemoveOverDrawFeature");
                urpAssetSetupInstance = Activator.CreateInstance(urpAssetSetupType);
            }
            catch (Exception ex)
            {
                ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, ProjScan.Modules.Module.Effects_Play_Check, ScanLog.Stage.s3, "URP ASSET", ex.GetType()?.ToString() + "\n" + ex.Message + "\n" + ex.StackTrace);
            }
        }

        public void SetRendererFeatureToData()
        {
            try
            {
                setRendererFeatureInfo.Invoke(urpAssetSetupInstance, urpDataPath);
            }
            catch (Exception ex)
            {
                ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, ProjScan.Modules.Module.Effects_Play_Check, ScanLog.Stage.s3, "URP ASSET", ex.GetType()?.ToString() + "\nAssign RendererFeatureFail\n" + ex.Message + "\n" + ex.StackTrace);
            }
        }

        public void SetUrpToSrpSetting()
        {
            try
            {
                setSrpSettingInfo.Invoke(urpAssetSetupInstance, urpAssetPath);
            }
            catch (Exception ex)
            {
                ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, ProjScan.Modules.Module.Effects_Play_Check, ScanLog.Stage.s3, "URP ASSET", ex.GetType()?.ToString() + "\nAssignSepSetting fail\n" + ex.Message + "\n" + ex.StackTrace);
            }
        }

        public void RemoveLastFeature()
        {
            try
            {
                removeOverDrawFeatureInfo.Invoke(urpAssetSetupInstance, null);
            }
            catch (Exception ex)
            {
                ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, ProjScan.Modules.Module.Effects_Play_Check, ScanLog.Stage.s3, "URP ASSET", ex.GetType()?.ToString() + "\nRemoveFeature fail\n" + ex.Message + "\n" + ex.StackTrace);
            }
        }

        public OverDrawUrpSupport()
        {
            urpFeatureLibFileName = "UrpFeatureLib";
        }
    }
}